﻿/*******************************************************
Use this dialog to replace browser's default alert\confirm.

var dialog = new CloudDisk.MessageDialog('content', 'title');
dialog.appendCommand('OK',  function(){
    alert('OK');
});
dialog.appendCommand('Cancel',  function(){
    alert('Cancel');
});
dialog.show();

*******************************************************/

CloudDisk.MessageDialog = function (content, title) {
    var el = $('<div class="msg-dialog"></div>');
    el.html(['<div class="msg-dialog-head"></div>',
              '<div class="msg-dialog-body"></div>',
              '<div class="msg-dialog-footer">',
                  '<button class="primaryButton"></button>',
                  '<button class="secondaryButton"></button>',
                  '<button class="secondaryButton"></button>',
              '</div>'].join(''));
    el.appendTo(document.body);

    el.find('.msg-dialog-body').html(content);
    if (title) {
		el.find('.msg-dialog-head').html(title).show();
    }
    
	CloudDisk.Util.showMask();


    this.el = el;
    this.buttons = el.find('button');
    this.defaultCommandIndex = 0;
    this.commands = [];
};

CloudDisk.MessageDialog.prototype = {
    appendCommand: function (text, handler) {
        if (this.commands.length > 2) {
            return false;
        }
        this.commands.push({
            text: text,
            handler: handler
        });
    },

    initButtons: function () {
        var dialog = this;
        var buttons = dialog.buttons;
        if (dialog.commands.length > 0) {
            for (var i = 0; i < dialog.commands.length && i < buttons.length; ++i) {
                buttons[i].innerHTML = dialog.commands[i].text;
                buttons[i].handler = dialog.commands[i].handler;
                buttons[i].addEventListener('click', function () {
                    if (this.handler) {
                        this.handler.call();
                    }
                    dialog.destroy();
                }, false);

                buttons[i].style.display = 'inline-block';
            }
        } else {
            buttons[2].innerHTML = '关闭';
            buttons[2].addEventListener('click', function () {
                dialog.destroy();
            }, false);
            buttons[2].style.display = 'inline-block';
        }

    },
	
	reposition: function(e){
	    var el = e.data.obj.el;
	    el.css('top', ($(window).height() - el.height()) / 2 + 'px');
	},

    show: function () {
        var dialog = this;
        dialog.defaultAction = function (e) {
            if (e.keyCode == 13) {//press enter
                if (dialog.commands.length > 0){
                    var handler = dialog.buttons[dialog.defaultCommandIndex].handler;
                    if (handler) {
                        handler.call();
                    }
                }
                dialog.destroy();
            } else if (e.keyCode == 9) {//press tab
                e.preventDefault();
                if (dialog.commands.length > 0) {                    
                    dialog.defaultCommandIndex = (dialog.defaultCommandIndex + 1) % dialog.commands.length;
                    dialog.buttons[dialog.defaultCommandIndex].focus();
                } else {
                    dialog.buttons[2].focus();
                }
            }
        }
        document.body.addEventListener('keydown', this.defaultAction);
		$(window).bind('resize', {obj: this}, this.reposition);

        this.initButtons();
              
        this.el.show();
        this.el.css('top', ($(window).height() - this.el.height()) / 2 + 'px');

        CloudDisk.MessageDialogMgr.add(this);
    },
    destroy: function () {
        document.body.removeEventListener('keydown', this.defaultAction);
		$(window).unbind('resize', this.reposition);

        var buttons = this.buttons;
        for (var i = 0; i < buttons.length; ++i) {
            delete buttons[i].handler;
        }
        delete this.buttons;

        this.el.remove();
        delete this.el;
        CloudDisk.Util.hideMask();
        CloudDisk.MessageDialogMgr.remove(this);
    }


};

CloudDisk.MessageDialogMgr = (function () {

    var idCounter = 0;
    var dialogs = {};

    return {
        add: function (o) {
            o.id = ++idCounter;
            dialogs[o.id] = o;
        },

        remove: function (o) {
            delete dialogs[o.id];
        },

        destroyAll: function () {
            for (var i in dialogs) {
                if (dialogs[i].el) {
                    dialogs[i].destroy();
                }
            }
            dialogs = [];
        }
    };
})();